﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brain.Skinning;

namespace Brain.FPS
{
    public class Weapon
    {
        public WeaponInfo Info;

        public WorldObject WorldObject;

        public int StoredBullets;
        public int CurrentClipBullets;

        private bool animated;
        public bool Animated
        {
            get { return animated; }
        }

        public Weapon(int ID)
        {
            this.Info = WeaponDatabase.Instance.GetWeaponInfo(ID);
            CurrentClipBullets = this.Info.ClipSize;
            Load();
        }
        public Weapon(WeaponInfo info)
        {
            this.Info = info;
            CurrentClipBullets = this.Info.ClipSize;
            Load();
        }

        private void Load()
        {
            if (!string.IsNullOrEmpty(this.Info.ModelPlace))
            {
                WorldObject = new WorldObject(this.Info.ModelPlace);
            }
            else
            {
                WorldObject = new WorldObject();
            }
            WorldObject.Rendering = WorldObjectRendering.Rendered;

            if (WorldObject.Model is SkinnedModel)
            {
                SkinnedModel model = WorldObject.Model as SkinnedModel;
                WorldObject.StartDefaultPlayer(model.AnimationClips[Info.IdleAnimationName]);

                animated = true;
            }
        }

        private int Timer;
        public void Update()
        {
            Timer += Engine.Instance.GameTime.ElapsedGameTime.Milliseconds;
            
            WorldObject.Update();
        }

        public bool Shoot()
        {
            if (Timer > Info.MsBetweenShots)
            {
                if (animated)
                {
                    SkinnedModel model = WorldObject.Model as SkinnedModel;
                    WorldObject.DefaultPlayer.StartClip(model.AnimationClips[Info.ShootAnimationName]);
                    WorldObject.DefaultPlayer.FinishedPlaying += new SetSkinnedAnimationPlayer(DefaultPlayer_FinishedPlaying);
                }

                Timer = 0;
                return true;
            }
            return false;
        }

        void DefaultPlayer_FinishedPlaying(SkinnedAnimationPlayer player)
        {
            if (animated)
            {
                SkinnedModel model = WorldObject.Model as SkinnedModel;
                player.StartClip(model.AnimationClips[Info.IdleAnimationName]);
            }
        }
        

        public void Draw()
        {
            WorldObject.Draw();
        }
    }
}
